package chapter10.eg13;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

public abstract class AbstractHttpXmlDecoder<T> extends MessageToMessageDecoder<T> {
  XStream xstream = new XStream(new StaxDriver());

  protected Object decode0(ChannelHandlerContext ctx, ByteBuf body) {
    byte[] bytes = new byte[body.readableBytes()];
    body.readBytes(bytes);
    String xml = new String(bytes);
    return xstream.fromXML(xml);
  }
}
